package com.jn.algorithm.day011.question35;

/**
 * @author 江南大学1033190417
 * @date 2022/4/26 21:27
 */
public class Solution {
    public int searchInsert(int[] nums, int target) {
        int length = nums.length;
        if (length == 0) {
            return 0;
        }
        if (length == 1) {
            return nums[0] >= target ? 0 : 1;
        }
        int left = 0, right = length - 1;
        while (left <= right) {
            int mid = (left + right) / 2;
            if (nums[mid] == target) {
                return mid;
            } else if (nums[mid] > target) {//target在左边
                right = mid - 1;
            } else {
                left = mid + 1;
            }
        }
        return left;
    }

    public static void main(String[] args) {
        Solution solution = new Solution();
        int[] nums = new int[]{1, 3, 5, 6};
        int target = 4;
        System.out.println(solution.searchInsert(nums, target));
    }
}
